Coding and Life

记录点点滴滴

嗨,我是徐斌,一位PHP开发者。


感谢您浏览我的博客

laravel mongodb 聚合查询

$results = User::raw(function ($collection) use ($userId) {
            return $collection->aggregate([
                [
                    '$match' => [
                        'id' => [ '$in' => $id ]
                    ]
                ],
                [
                    '$group' => [
                        '_id'   => '$user_id',
                        "total" => [ '$sum' => '$price' ]
                    ]
                ],
            ]);
        });

match 是匹配的条件

$gt -------- greater than  >

$gte --------- gt equal  >=

$lt -------- less than  <

$lte --------- lt equal  <=

$ne ----------- not equal  !=

$eq  --------  equal  =

group 分组条件 total 统计总数($price 替换成1)或者总和($price 替换成指定字段)

最近的文章

Git 常用命令配置

继续阅读
更早的文章

zookeeper

继续阅读